<template>
  <div>
    <el-form>
      <el-form-item label="教师姓名">
        <el-input v-model="pojo.name" style="width: 200px"></el-input>
      </el-form-item>

      <el-form-item label="班级">
        <el-select v-model="pojo.rid" placeholder="请选择班级" clearable>
          <el-option v-for="r in rooms" :label="r.name" :value="r.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="入职时间">
        <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="pojo.start">
        </el-date-picker>
        <el-col class="line" :span="2">-</el-col>
        <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="pojo.end">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="findPage">查询</el-button>
        <el-button type="primary" @click="toAdd">添加</el-button>
        <el-button type="primary" @click="deleteIdArr">选中删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="id"
          label="ID">
      </el-table-column>

      <el-table-column
          prop="name"
          label="姓名">
      </el-table-column>

      <el-table-column
          prop="sex"
          label="性别">
        <template slot-scope="scope">{{ scope.row.sex == 0 ? "男" : "女" }}</template>
      </el-table-column>

      <el-table-column
          prop="hobby"
          label="爱好">
      </el-table-column>

      <el-table-column
          prop="time"
          label="入职时间"
          width="120">
        <template slot-scope="scope">{{ scope.row.time }}</template>
      </el-table-column>

      <el-table-column
          prop="rname"
          label="教室">
      </el-table-column>

      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toUpdate(scope.row)">修改</el-button>
          <el-button type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal">
    </el-pagination>

    <el-dialog :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">

        <el-form-item label="教师姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="爱好">
          <el-checkbox-group v-model="hobbyArr">
            <el-checkbox label="睡觉" name="hobby"></el-checkbox>
            <el-checkbox label="看书" name="hobby"></el-checkbox>
            <el-checkbox label="喝茶" name="hobby"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="入职时间">
          <el-col :span="11">
            <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.time"
                style="width: 100%;">
            </el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item label="教室">
          <el-select v-model="form.rid" placeholder="请选择教室">
            <el-option v-for="r in rooms" :label="r.name" :value="r.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">提 交</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pojo: {},
      rooms: [],
      tableData: [],
      idArr: [],
      pageNum: 1,
      pageSize: 2,
      pageTotal: 0,
      dialogVisible: false,
      form: {},
      hobbyArr: [],
    }
  },
  methods: {
    findPage() {
      this.axios.post(`/teacher/findPage?pn=${this.pageNum}&ps=${this.pageSize}`, this.pojo).then(res => {
        this.tableData = res.data.records;
        this.pageTotal = res.data.total;
      })
    },
    findRoom() {
      this.axios.post("/room/findAll").then(res => {
        this.rooms = res.data;
      })
    },
    toAdd() {
      this.hobbyArr = [];
      this.form = {};
      this.dialogVisible = true;
    },
    deleteIdArr() {
      this.form.hobby = this.hobbyArr.join(",");
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post("/teacher/deleteIdArr", this.idArr).then(res => {
          if (res.data) {
            this.$message.success("删除成功");
            this.findPage();
          } else {
            this.$message.error("删除失败");
          }
        })
      })
    },
    handleSelectionChange(val) {
      for (let i = 0; i < val.length; i++) {
        this.idArr.push(val[i].id);
      }
    },
    toUpdate(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.hobbyArr = this.form.hobby.split(",")
      this.dialogVisible = true;
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post("/teacher/deleteById?id=" + id).then(res => {
          if (res.data) {
            this.$message("删除成功")
            this.findPage();
          } else {
            this.$message("删除失败")
          }
        })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.findPage();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.findPage();
    },
    onSubmit() {
      this.form.hobby = this.hobbyArr.join(",");
      this.axios.post("/teacher/upadd", this.form).then(res => {
        if (res.data) {
          this.$message("添加/修改成功");
          this.dialogVisible = false;
          this.findPage();
        } else {
          this.dialogVisible = false;
          this.$message("添加/修改失败");
        }
      })
    },
  },
  created() {
    this.findPage();
    this.findRoom();
  },
  watch: {
    req: {
      deep: true,
      handler() {
        this.findPage();
      }
    }
  }
}
</script>
